package py.com.alfa.escriba.web.rest.impl;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;

import java.util.List;

import py.com.alfa.escriba.config.HttpHeaderConfig;
import py.com.alfa.escriba.config.EscribaApplicationContextProvider;
import py.com.alfa.escriba.exception.ErrorCode;
import py.com.alfa.escriba.exception.ServiceException;
import py.com.alfa.escriba.util.JSONTranslator;
import py.com.alfa.escriba.web.interceptor.BeanNames;
import py.com.alfa.escriba.web.interceptor.InvocationContext;
import py.com.alfa.escriba.web.rest.secure.InmueblesSecureRESTService;
import py.com.alfa.escriba.web.services.InmueblesService;
import py.com.alfa.escriba.persistence.dto.InmueblesDTO;
import py.com.alfa.escriba.persistence.dto.VehiculosDTO;

public class InmueblesSecureRESTServiceImpl implements InmueblesSecureRESTService{

  private ApplicationContext ctx;
  private InmueblesService dao;
  private InvocationContext ic;
  private HttpHeaderConfig config;
  private Logger logger = LoggerFactory.getLogger(getClass());

  public InmueblesSecureRESTServiceImpl() {
    this.ctx = EscribaApplicationContextProvider.getContext();
    this.dao = (InmueblesService) ctx.getBean("inmueblesService");
    this.config = (HttpHeaderConfig) ctx.getBean(BeanNames.HTTP_HEADERS_BEAN);
  }

  @Override
  public List<InmueblesDTO> listInmuebles(HttpServletRequest request) throws ServiceException { 
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      List<InmueblesDTO> result = dao.listInmuebles(ic);
      return result;
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }

  @Override
  public InmueblesDTO showInmuebles(Long id, HttpServletRequest request) throws ServiceException { 
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      InmueblesDTO result = dao.getInmuebles(id, ic);
      return result;
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }

  @Override
  public InmueblesDTO saveInmuebles(Long id, InmueblesDTO dto, HttpServletRequest request) throws ServiceException {
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      InmueblesDTO result = dao.updateInmuebles(id, dto, ic);
      return result;
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }

  @Override
  public InmueblesDTO createInmuebles(InmueblesDTO dto, HttpServletRequest request) throws ServiceException {
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      InmueblesDTO result = dao.createInmuebles(dto, ic);
      return result;
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }

  @Override
  public Response deleteInmuebles(Long id, HttpServletRequest request) throws ServiceException {
    try {
      ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
      dao.deleteInmuebles(id, ic);
      return Response.ok().build();
    }
    catch (ServiceException e){
      logger.error(e.getMessage(), e);
      throw e;
    }
    catch (Exception e){
      logger.error(e.getMessage(), e);
      throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
    }
  }

@Override
public List<InmueblesDTO> buscarInmuebles(InmueblesDTO dto,	HttpServletRequest request) throws ServiceException {
	try {
		JSONTranslator<InmueblesDTO> trans = new JSONTranslator<InmueblesDTO>(InmueblesDTO.class);
		logger.info("INMUEBLE:_{}", trans.toJSON(dto));
		ic = (InvocationContext) request.getAttribute(this.config.getContextAttributeName());
		List<InmueblesDTO> list = this.dao.buscarInmuebles(dto, ic);
		return list;
	}
	catch (ServiceException e){
		logger.error(e.getMessage(), e);
		throw e;
	}
	catch (Exception e){
		logger.error(e.getMessage(), e);
		throw new ServiceException(ErrorCode.INTERNAL_ERROR, "internal.dataAccessError");
	}
}

}